package com.oa.im.DaoImpl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.oa.entity.TblPersonalInformation;
import com.oa.im.Dao.ImDao;
/**
 * 个人信息数据操作接口
 * @author Bond
 * @date 2015年10月10日 11:34:20
 */
public class ImDaoImpl implements ImDao {
	
	private HibernateTemplate hibernateTemplate;
	
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
	/*
	 * 加载个人信息
	 * @see com.oa.im.Dao.ImDao#load(java.lang.Integer)
	 */
	@Override
	public TblPersonalInformation load(Integer Id) {
		// TODO Auto-generated method stub
		TblPersonalInformation im = new TblPersonalInformation();
		try{
			im = (TblPersonalInformation) this.hibernateTemplate.load(TblPersonalInformation.class, Id);
		}catch(Exception e){
			e.printStackTrace();
		}
		return im;
	}
	/*
	 * 添加个人信息
	 * @see com.oa.im.Dao.ImDao#add(com.oa.entity.TblPersonalInformation)
	 */
	@Override
	public boolean add(TblPersonalInformation tpi) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try{
			this.hibernateTemplate.save(tpi);
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}
	/*
	 * 删除个人信息
	 * @see com.oa.im.Dao.ImDao#delete(com.oa.entity.TblPersonalInformation)
	 */
	@Override
	public boolean delete(TblPersonalInformation tpi) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try{
			this.hibernateTemplate.delete(tpi);
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}
	/*
	 * 更新个人信息
	 * @see com.oa.im.Dao.ImDao#update(com.oa.entity.TblPersonalInformation)
	 */
	@Override
	public boolean update(TblPersonalInformation tpi) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try{
			this.hibernateTemplate.update(tpi);
		}catch(Exception e){
			e.printStackTrace();
		}
		return this.update(tpi);
	}
	/*
	 * 数据查询
	 * @see com.oa.im.Dao.ImDao#searchData(java.lang.String, java.util.Map)
	 */
	@Override
	public List searchData(String hql, Map params) {
		// TODO Auto-generated method stub
		Query query = null;
		List list = null;
		try {
			if (params != null&&params.size() > 0) {
				query = this.hibernateTemplate.getSessionFactory().openSession().createQuery(hql);
				Object[] keyArrs = params.keySet().toArray();
				for (int i = 0; i < keyArrs.length; i++) {
					query.setParameter(keyArrs[i].toString(),params.get(keyArrs[i]));
				}
				list = query.list();
			} else {
				list = this.hibernateTemplate.find(hql);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

}
